package com.ruoyi.project.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.system.domain.SysProduct;
import com.ruoyi.project.system.domain.SysUserProductPrice;
import com.ruoyi.project.system.mapper.SysProductMapper;
import com.ruoyi.project.system.service.ISysProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 产品Service业务层处理
 *
 */
@Service
public class SysProductServiceImpl implements ISysProductService {

    @Autowired
    private SysProductMapper sysProductMapper;

    @Override
    public Float getPricing(Integer userId, Integer productId) {
        Float pricing = sysProductMapper.customizedPricing(userId, productId);
        if (pricing == null) {
            return sysProductMapper.selectSysProductByProductId(productId).getPricing().floatValue();
        } else {
            return pricing;
        }
    }

    @Override
    public Float getFee(Integer userId, Integer productId) {
        Float fee = sysProductMapper.customizedFee(userId, productId);
        if (fee == null) {
            return sysProductMapper.selectSysProductByProductId(productId).getFee().floatValue();
        } else {
            return fee;
        }
    }

    @Override
    public SysUserProductPrice getUserProductPrice(Integer userId, Integer productId) {
        SysUserProductPrice userProductPrice = sysProductMapper.customizedSiyuUserPrice(userId, productId);
        SysProduct sysProduct = null;
        if (userProductPrice == null) {
            sysProduct = sysProductMapper.selectSysProductByProductId(productId);
            return new SysUserProductPrice(userId, productId, sysProduct);
        } else if (userProductPrice.getPricing() == null || userProductPrice.getFee() == null) {
            sysProduct = sysProductMapper.selectSysProductByProductId(productId);
        }

        if (userProductPrice.getPricing() == null) {
            userProductPrice.setPricing(sysProduct.getPricing());
        }
        if (userProductPrice.getFee() == null) {
            userProductPrice.setFee(sysProduct.getFee());
        }
        return userProductPrice;
    }

    /**
     * 查询产品
     *
     * @param productId 产品主键
     * @return 产品
     */
    @Override
    public SysProduct selectSysProductByProductId(int productId) {
        return sysProductMapper.selectSysProductByProductId(productId);
    }


    /**
     * 修改产品
     *
     * @param sysProduct 产品
     * @return 结果
     */
    @Override
    public int updateSysProduct(SysProduct sysProduct) {
        sysProduct.setUpdateTime(DateUtils.getNowDate());
        return sysProductMapper.updateSysProduct(sysProduct);
    }

}
